﻿/*
 * Created by: 
 * Created: Friday, December 15, 2006
 */

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Edge.Common.Web.Controls.Ext
{
    /// <summary>
    /// Control contain fixes and new fatures for statndart control System.Web.UI.WebControls.HyperLink
    /// fixes:
    /// 1) No retive navigation by default. For enable relaytive navigation assign Relative value true.
    /// features:
    /// 1) Trunk link text by length (TODO trnuk by length and words)
    ///		1.1) Promt full text
    ///		1.2) Append ... to trunkated text (to change to another strig ypu must change property TrunkAppendText )
    /// </summary>
    public class Hyperlink : System.Web.UI.WebControls.HyperLink
    {
        private bool _relative = false;
        private bool _trunkText = false;
        private int _trunkLength = 0;
        private bool _promtTrunkatedText = true;
        private string _tipText = string.Empty;
        private string _trunkAppendText = "...";
        private bool label = false;

        public Hyperlink()
        {

        }

        [Bindable(true), DefaultValue(false)]
        public virtual bool IsLabel
        {
            get { return label; }
            set { label = value; }
        }

        [Bindable(true), DefaultValue(false)]
        public bool Relative
        {
            get { return _relative; }
            set { _relative = value; }
        }

        [Bindable(true), DefaultValue(false)]
        public bool TrunkText
        {
            get { return _trunkText; }
            set { _trunkText = value; }
        }

        [Bindable(true), DefaultValue(0)]
        public int TrunkLength
        {
            get { return _trunkLength; }
            set { _trunkLength = value; }
        }

        [Bindable(true), DefaultValue(true)]
        public bool PromtTrunkatedText
        {
            get { return _promtTrunkatedText; }
            set { _promtTrunkatedText = value; }
        }

        [Bindable(true), DefaultValue("...")]
        public string TrunkAppendText
        {
            get { return _trunkAppendText; }
            set { _trunkAppendText = value; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            // hack
            string url = NavigateUrl;
            if (!Relative)
            {
                NavigateUrl = string.Empty;
            }
            base.AddAttributesToRender(writer);

            if (!Relative)
            {
                NavigateUrl = url;
                writer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
            }

            if (_tipText != string.Empty)
                writer.AddAttribute(HtmlTextWriterAttribute.Title, _tipText);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (TrunkText)
            {
                if (TrunkLength <= 0)
                    throw new ArgumentException("TrunkLength");

                if (PromtTrunkatedText && Text.Length > TrunkLength)
                {
                    _tipText = Text;
                    Text = Text.Substring(0, TrunkLength) + TrunkAppendText;
                }

                //				else
                //					_tipText = string.Empty;

            }
            base.OnPreRender(e);

        }

        /// <summary> 
        /// display text box or label
        /// </summary>
        protected override void Render(HtmlTextWriter output)
        {

            if (!IsLabel) base.Render(output);
            else
            {
                Label lb = new Label();
                lb.ID = this.ID;
                lb.Text = this.Text;
                if (lb.Text == null || lb.Text.Trim().Length == 0) lb.Text = "&nbsp;";
                lb.RenderControl(output);
            }

        }
    }
}